package com.school.empsys.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 */
public class DateUtils {

    public static interface DateFormatPattern {
        String PATTERN_1 = "yyyy-MM-dd";
        String PATTERN_2 = "yyyy-MM-dd HH:mm:ss";
        String PATTERN_3 = "HH:mm:ss";
        String PATTERN_4 = "yyyy-MM-dd HH:mm:ss.SSS";
        String PATTERN_5 = "yyyy-MM";
    }

    /**
     * 得到当前时间后的字符串
     *
     * @return
     */
    public static String nowString() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
    }

    /**
     * 字符串转为Date
     *
     * @param str
     * @param pattern
     * @return
     */
    public static Date string2Date(String str, String pattern) {
        if (StrUtil.isEmpty(str) || StrUtil.isEmpty(pattern)) {
            return null;
        }
        try {
            return new SimpleDateFormat(pattern).parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String date2String(Date date, String pattern) {
        if (date == null || StrUtil.isEmpty(pattern)) {
            return null;
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    public static Date date2PatternDate(Date source, String pattern) {
        if (source == null) {
            return null;
        }
        return string2Date(date2String(source, pattern), pattern);
    }

    public static Date getEndTime(int year, int month) {
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate endOfMonth = yearMonth.atEndOfMonth();
        LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取当月开始时间
     * @return
     */
    public static Date getMonthStartTime(Date date) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
//        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    /**
     * 获取当月的结束时间
     * @return
     */
    public static Date getMonthEndTime(Date date) {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
//        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static BigDecimal dateDifferHours(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        long differ = (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / 1000 / (60 * 60);
        return new BigDecimal(differ);
    }

    public static BigDecimal dateDifferDays(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        long differ = (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / 1000 / (60 * 60 * 24);
        return new BigDecimal(differ);
    }

    public static Date date2DateTime(Date date, String time) {
        if (StrUtil.isEmpty(time)) {
            return null;
        }
        String dateTime = new SimpleDateFormat(DateFormatPattern.PATTERN_1).format(date) + " " + time;
        try {
            return new SimpleDateFormat(DateFormatPattern.PATTERN_2).parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {

        System.out.println(getMonthEndTime(string2Date("2020-07", "yyyy-MM")));
        System.out.println(getMonthStartTime(string2Date("2020-07", "yyyy-MM")));
    }

}

